/*
 * é Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.view.data;

import java.awt.BorderLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Insets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JComboBox;

import org.vineatoum.business.Parcelle;
import org.vineatoum.business.Plantation;
import org.vineatoum.business.configuration.Appellation;
import org.vineatoum.business.configuration.Cepage;

import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.view.utils.JPanelWrapped;

import com.toedter.calendar.JYearChooser;

public class PlantationDataPanel extends JPanelWrapped{

	/** Default ID version */
	private static final long serialVersionUID = 2211134534707770635L;
	private JTextField textFieldName;
	private JComboBox comboBoxCepage;
	private JComboBox comboBoxAppelation;
	private JSpinner spinnerSurface;
	private JYearChooser yearChooserPlantationYear;
	private JSpinner spinnerCepGap;
	private JSpinner spinnerRowGap;
	private JSpinner spinnerCepDensity;
	private JList listBoxParcelles;

	public PlantationDataPanel(IVineaToumModel model, Plantation plantation) {
		super();
		
		setBounds(100, 100, 450, 300);
		setLayout(new BorderLayout(0, 0));

		{
			GridBagLayout gbl_panel_1 = new GridBagLayout();
			gbl_panel_1.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0 };
			gbl_panel_1.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
			gbl_panel_1.columnWeights = new double[] { 0.0, 1.0, 1.0, 0.0, 1.0,
					1.0, Double.MIN_VALUE };
			gbl_panel_1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0,
					0.0, Double.MIN_VALUE };
			setLayout(gbl_panel_1);
		}
		{
			JLabel lblNom = new JLabel("Nom");
			GridBagConstraints gbc_lblNom = new GridBagConstraints();
			gbc_lblNom.insets = new Insets(0, 0, 5, 5);
			gbc_lblNom.anchor = GridBagConstraints.EAST;
			gbc_lblNom.gridx = 0;
			gbc_lblNom.gridy = 0;
			add(lblNom, gbc_lblNom);
		}
		{
			JLabel lblYearPlantation = new JLabel("Année de plantation");
			GridBagConstraints gbc_lblLieuDit = new GridBagConstraints();
			gbc_lblLieuDit.anchor = GridBagConstraints.EAST;
			gbc_lblLieuDit.insets = new Insets(0, 0, 5, 5);
			gbc_lblLieuDit.gridx = 3;
			gbc_lblLieuDit.gridy = 0;
			add(lblYearPlantation, gbc_lblLieuDit);
		}
		{
			setYearChooserPlantationYear(new JYearChooser());
			getYearChooserPlantationYear().setYear(plantation.getPlantationYear());

			GridBagConstraints gbc_textField_1 = new GridBagConstraints();
			gbc_textField_1.insets = new Insets(0, 0, 5, 5);
			gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_1.gridx = 4;
			gbc_textField_1.gridy = 0;
			add(getYearChooserPlantationYear(), gbc_textField_1);
		}
		{
			setTextFieldName(new JTextField());
			GridBagConstraints gbc_textField = new GridBagConstraints();
			gbc_textField.insets = new Insets(0, 0, 5, 5);
			gbc_textField.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField.gridx = 1;
			gbc_textField.gridy = 0;
			add(getTextFieldName(), gbc_textField);
			getTextFieldName().setColumns(10);
		}
		{
			JLabel lblCpage = new JLabel("C\u00E9page");
			GridBagConstraints gbc_lblCpage = new GridBagConstraints();
			gbc_lblCpage.anchor = GridBagConstraints.EAST;
			gbc_lblCpage.insets = new Insets(0, 0, 5, 5);
			gbc_lblCpage.gridx = 0;
			gbc_lblCpage.gridy = 1;
			add(lblCpage, gbc_lblCpage);
		}
		{
			setComboBoxCepage(new JComboBox());
			List<Cepage> data = new ArrayList<Cepage>();
			for (Iterator<Cepage> iterator = Cepage.cepages.iterator(); iterator
					.hasNext();) {
				Cepage it = (Cepage) iterator.next();
				if (it.getName() != null && !it.getName().isEmpty()) {
					data.add(it);
				}
			}
			getComboBoxCepage().setModel(new DefaultComboBoxModel(data.toArray()));

			GridBagConstraints gbc_textField_2 = new GridBagConstraints();
			gbc_textField_2.insets = new Insets(0, 0, 5, 5);
			gbc_textField_2.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_2.gridx = 1;
			gbc_textField_2.gridy = 1;
			add(getComboBoxCepage(), gbc_textField_2);
		}
		{
			JLabel lblAppelation = new JLabel("Appelation");
			GridBagConstraints gbc_lblAppelation = new GridBagConstraints();
			gbc_lblAppelation.anchor = GridBagConstraints.EAST;
			gbc_lblAppelation.insets = new Insets(0, 0, 5, 5);
			gbc_lblAppelation.gridx = 3;
			gbc_lblAppelation.gridy = 1;
			add(lblAppelation, gbc_lblAppelation);
		}
		{

			setComboBoxAppelation(new JComboBox());
			List<Appellation> data = new ArrayList<Appellation>();
			for (Iterator<Appellation> iterator = Appellation.appellations
					.iterator(); iterator.hasNext();) {
				Appellation it = (Appellation) iterator.next();
				if (it.getName() != null && !it.getName().isEmpty()) {
					data.add(it);
				}
			}
			getComboBoxAppelation()
					.setModel(new DefaultComboBoxModel(data.toArray()));
			GridBagConstraints gbc_textField_3 = new GridBagConstraints();
			gbc_textField_3.insets = new Insets(0, 0, 5, 5);
			gbc_textField_3.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_3.gridx = 4;
			gbc_textField_3.gridy = 1;
			add(getComboBoxAppelation(), gbc_textField_3);
		}
		{
			JLabel lblSurface = new JLabel("Surface");
			GridBagConstraints gbc_lblSurface = new GridBagConstraints();
			gbc_lblSurface.anchor = GridBagConstraints.EAST;
			gbc_lblSurface.insets = new Insets(0, 0, 5, 5);
			gbc_lblSurface.gridx = 0;
			gbc_lblSurface.gridy = 3;
			add(lblSurface, gbc_lblSurface);
		}
		{
			setSpinnerSurface(new JSpinner());
			getSpinnerSurface().setModel(new SpinnerNumberModel(new Float(0), new Float(0), new Float(9999), new Float(0.5)));
			GridBagConstraints gbc_textField_4 = new GridBagConstraints();
			gbc_textField_4.insets = new Insets(0, 0, 5, 5);
			gbc_textField_4.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_4.gridx = 1;
			gbc_textField_4.gridy = 3;
			add(getSpinnerSurface(), gbc_textField_4);
		}
		{
			JLabel lblSurfaceMetric = new JLabel("ha");
			GridBagConstraints gbc_lblSurface = new GridBagConstraints();
			gbc_lblSurface.anchor = GridBagConstraints.WEST;
			gbc_lblSurface.insets = new Insets(0, 0, 5, 5);
			gbc_lblSurface.gridx = 2;
			gbc_lblSurface.gridy = 3;
			add(lblSurfaceMetric, gbc_lblSurface);
		}
		{
			JLabel lblEcartementEntreCep = new JLabel("Ecartement entre cep");
			GridBagConstraints gbc_lblEcartementEntreCep = new GridBagConstraints();
			gbc_lblEcartementEntreCep.anchor = GridBagConstraints.EAST;
			gbc_lblEcartementEntreCep.insets = new Insets(0, 0, 5, 5);
			gbc_lblEcartementEntreCep.gridx = 0;
			gbc_lblEcartementEntreCep.gridy = 4;
			add(lblEcartementEntreCep, gbc_lblEcartementEntreCep);
		}
		{
			setSpinnerCepGap(new JSpinner());
			getSpinnerCepGap().setModel(new SpinnerNumberModel(new Float(0), new Float(0), new Float(99999), new Float(0.5)));
			GridBagConstraints gbc_textField_7 = new GridBagConstraints();
			gbc_textField_7.insets = new Insets(0, 0, 5, 5);
			gbc_textField_7.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_7.gridx = 1;
			gbc_textField_7.gridy = 4;
			add(getSpinnerCepGap(), gbc_textField_7);
		}
		{
			JLabel lblEcartementEntreCep = new JLabel("m");
			GridBagConstraints gbc_lblEcartementEntreCep = new GridBagConstraints();
			gbc_lblEcartementEntreCep.anchor = GridBagConstraints.WEST;
			gbc_lblEcartementEntreCep.insets = new Insets(0, 0, 5, 5);
			gbc_lblEcartementEntreCep.gridx = 2;
			gbc_lblEcartementEntreCep.gridy = 4;
			add(lblEcartementEntreCep, gbc_lblEcartementEntreCep);
		}
		{
			JLabel lblEcartDeRang = new JLabel("Ecart de rang");
			GridBagConstraints gbc_lblEcartDeRang = new GridBagConstraints();
			gbc_lblEcartDeRang.anchor = GridBagConstraints.EAST;
			gbc_lblEcartDeRang.insets = new Insets(0, 0, 5, 5);
			gbc_lblEcartDeRang.gridx = 3;
			gbc_lblEcartDeRang.gridy = 4;
			add(lblEcartDeRang, gbc_lblEcartDeRang);
		}
		{
			setSpinnerRowGap(new JSpinner());
			getSpinnerRowGap().setModel(new SpinnerNumberModel(new Float(0), new Float(0), new Float(99999), new Float(0.5)));
			GridBagConstraints gbc_textField_6 = new GridBagConstraints();
			gbc_textField_6.insets = new Insets(0, 0, 5, 5);
			gbc_textField_6.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_6.gridx = 4;
			gbc_textField_6.gridy = 4;
			add(getSpinnerRowGap(), gbc_textField_6);
		}
		{
			JLabel lblEcartDeRang = new JLabel("m");
			GridBagConstraints gbc_lblEcartDeRang = new GridBagConstraints();
			gbc_lblEcartDeRang.anchor = GridBagConstraints.WEST;
			gbc_lblEcartDeRang.insets = new Insets(0, 0, 5, 5);
			gbc_lblEcartDeRang.gridx = 5;
			gbc_lblEcartDeRang.gridy = 4;
			add(lblEcartDeRang, gbc_lblEcartDeRang);
		}
		{
			JLabel lblDensitDeCep = new JLabel("Densit\u00E9 de cep");
			GridBagConstraints gbc_lblDensitDeCep = new GridBagConstraints();
			gbc_lblDensitDeCep.anchor = GridBagConstraints.EAST;
			gbc_lblDensitDeCep.insets = new Insets(0, 0, 0, 5);
			gbc_lblDensitDeCep.gridx = 0;
			gbc_lblDensitDeCep.gridy = 5;
			add(lblDensitDeCep, gbc_lblDensitDeCep);
		}
		{
			setSpinnerCepDensity(new JSpinner());
			getSpinnerCepDensity().setModel(new SpinnerNumberModel(new Integer(0), new Integer(0), new Integer(999999), new Integer(100)));
			GridBagConstraints gbc_textField_8 = new GridBagConstraints();
			gbc_textField_8.insets = new Insets(0, 0, 0, 5);
			gbc_textField_8.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_8.gridx = 1;
			gbc_textField_8.gridy = 5;
			add(getSpinnerCepDensity(), gbc_textField_8);
		}
		{
			JLabel lblDensitDeCep = new JLabel("Ceps/ha");
			GridBagConstraints gbc_lblDensitDeCep = new GridBagConstraints();
			gbc_lblDensitDeCep.anchor = GridBagConstraints.WEST;
			gbc_lblDensitDeCep.insets = new Insets(0, 0, 0, 5);
			gbc_lblDensitDeCep.gridx = 2;
			gbc_lblDensitDeCep.gridy = 5;
			add(lblDensitDeCep, gbc_lblDensitDeCep);
		}
		{
			JLabel lblParcelles = new JLabel("Parcelles");
			GridBagConstraints gbc_lblDensitDeCep = new GridBagConstraints();
			gbc_lblDensitDeCep.anchor = GridBagConstraints.EAST;
			gbc_lblDensitDeCep.insets = new Insets(0, 0, 0, 5);
			gbc_lblDensitDeCep.gridx = 0;
			gbc_lblDensitDeCep.gridy = 6;
			add(lblParcelles, gbc_lblDensitDeCep);
		}
		{
			List<Parcelle> data = new ArrayList<Parcelle>();
			for (Iterator<Parcelle> iterator = Parcelle.parcelles.iterator(); iterator.hasNext();) {
				Parcelle type = (Parcelle) iterator.next();
				if(type.getName() != null && !type.getName().isEmpty()) {
					data.add(type);
				}
			}
			setListBoxParcelles(new JList(data.toArray()));
			
			getListBoxParcelles().setVisibleRowCount(8);
			JScrollPane optionPane = new JScrollPane(getListBoxParcelles());

			GridBagConstraints gbc_textField_8 = new GridBagConstraints();
			gbc_textField_8.insets = new Insets(0, 0, 0, 5);
			gbc_textField_8.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_8.gridx = 1;
			gbc_textField_8.gridy = 6;
			add(optionPane, gbc_textField_8);
		}
		
		updateFields(plantation);
	}
	
	public void updateFields(Plantation plantation) {
		getTextFieldName().setText(plantation.getName());
		getComboBoxCepage().setSelectedItem(plantation.getCepage());
		getComboBoxAppelation().setSelectedItem(plantation.getAppellation());
		getSpinnerSurface().setValue(plantation.getSurfaceArea());
		getSpinnerCepGap().setValue(plantation.getCepGap());
		getSpinnerRowGap().setValue(plantation.getRowGap());
		getSpinnerCepDensity().setValue(plantation.getCepDensity());
		if(plantation.getParcelles()!= null&& !plantation.getParcelles().isEmpty())	
		{
			for (Iterator<Parcelle> iterator = plantation.getParcelles().iterator(); iterator.hasNext();) {
			
				Parcelle parcelle = (Parcelle) iterator.next();
				
				getListBoxParcelles().setSelectedValue(parcelle, true);
			}
		}
		
		
	}

	@Override
	public void doWhenWrapperOkClick() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doWhenWrapperCancelClick() {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @param textFieldName the textFieldName to set
	 */
	public void setTextFieldName(JTextField textFieldName) {
		this.textFieldName = textFieldName;
	}

	/**
	 * @return the textFieldName
	 */
	public JTextField getTextFieldName() {
		return textFieldName;
	}

	/**
	 * @param comboBoxCepage the comboBoxCepage to set
	 */
	public void setComboBoxCepage(JComboBox comboBoxCepage) {
		this.comboBoxCepage = comboBoxCepage;
	}

	/**
	 * @return the comboBoxCepage
	 */
	public JComboBox getComboBoxCepage() {
		return comboBoxCepage;
	}

	/**
	 * @param comboBoxAppelation the comboBoxAppelation to set
	 */
	public void setComboBoxAppelation(JComboBox comboBoxAppelation) {
		this.comboBoxAppelation = comboBoxAppelation;
	}

	/**
	 * @return the comboBoxAppelation
	 */
	public JComboBox getComboBoxAppelation() {
		return comboBoxAppelation;
	}

	/**
	 * @param spinnerSurface the spinnerSurface to set
	 */
	public void setSpinnerSurface(JSpinner spinnerSurface) {
		this.spinnerSurface = spinnerSurface;
	}

	/**
	 * @return the spinnerSurface
	 */
	public JSpinner getSpinnerSurface() {
		return spinnerSurface;
	}

	/**
	 * @param yearChooserPlantationYear the yearChooserPlantationYear to set
	 */
	public void setYearChooserPlantationYear(JYearChooser yearChooserPlantationYear) {
		this.yearChooserPlantationYear = yearChooserPlantationYear;
	}

	/**
	 * @return the yearChooserPlantationYear
	 */
	public JYearChooser getYearChooserPlantationYear() {
		return yearChooserPlantationYear;
	}

	/**
	 * @param spinnerCepGap the spinnerCepGap to set
	 */
	public void setSpinnerCepGap(JSpinner spinnerCepGap) {
		this.spinnerCepGap = spinnerCepGap;
	}

	/**
	 * @return the spinnerCepGap
	 */
	public JSpinner getSpinnerCepGap() {
		return spinnerCepGap;
	}

	/**
	 * @param spinnerRowGap the spinnerRowGap to set
	 */
	public void setSpinnerRowGap(JSpinner spinnerRowGap) {
		this.spinnerRowGap = spinnerRowGap;
	}

	/**
	 * @return the spinnerRowGap
	 */
	public JSpinner getSpinnerRowGap() {
		return spinnerRowGap;
	}

	/**
	 * @param spinnerCepDensity the spinnerCepDensity to set
	 */
	public void setSpinnerCepDensity(JSpinner spinnerCepDensity) {
		this.spinnerCepDensity = spinnerCepDensity;
	}

	/**
	 * @return the spinnerCepDensity
	 */
	public JSpinner getSpinnerCepDensity() {
		return spinnerCepDensity;
	}

	/**
	 * @param listBoxParcelles the listBoxParcelles to set
	 */
	public void setListBoxParcelles(JList listBoxParcelles) {
		this.listBoxParcelles = listBoxParcelles;
	}

	/**
	 * @return the listBoxParcelles
	 */
	public JList getListBoxParcelles() {
		return listBoxParcelles;
	}

}
